package com.example.recorder;

import android.util.Log;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class AudioSender implements Callable<String> {
    private final String TAG = AudioSender.class.getSimpleName();
    private String outputFile;

    public AudioSender(String fileLocation) {
        outputFile = fileLocation;
    }

    public String call() {
        File output = new File(outputFile);
        String response = new String();
        try {
            Log.d(TAG, "开始上传");
            MultipartUtility multipart = new MultipartUtility("http://192.168.2.15:3000/upload");
            multipart.addFilePart("audio", output);
            response = multipart.finish();
            Log.d(TAG, "上传结束");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            Log.d(TAG, "SERVER REPLIED");
            return response;
        }

    }
    public void upload() {
        File output = new File(outputFile);
        HttpUtil.postFile("http://192.168.2.15:8001/upload", output, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                return;
            }
        });
    }
}



